package double_pointer;

public class trap {
    public int trap(int[] height) {
        int left = 0;
        int right = height.length - 1;
        int ret= 0;
        int leftMax = 0;
        int rightMax = 0;
        while(left < right) {
            leftMax = Math.max(leftMax,height[left]);
            rightMax = Math.max(rightMax,height[right]);
            if(height[left] < height[right]){
                ret += leftMax-height[left];
                left++;
            }else {
                ret += rightMax-height[right];
                right--;
            }
        }
        return ret;
    }
}
